package cn.stylefeng.guns.modular.single.controller;

import cn.stylefeng.guns.modular.single.Param.PatientParam;
import cn.stylefeng.guns.modular.single.entity.DiseaseType;
import cn.stylefeng.guns.modular.single.entity.Patient;
import cn.stylefeng.guns.modular.single.service.DiseaseTypeService;
import cn.stylefeng.guns.modular.single.service.PatientService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.config.modular.pojo.param.SysConfigParam;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.annotation.ApiLog;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserOrg;
import cn.stylefeng.roses.kernel.sys.modular.user.mapper.SysUserOrgMapper;
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserOrgService;
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@ApiResource(
        name = "病种的接口"
)
public class PatientController {
    @Resource
    private PatientService service;
    @Resource
    private SysUserOrgMapper sysUserOrgMapper;
    @GetResource(
            name = "患者列表",
            path = {"/patients/page"}
    )
    public ResponseData<PageResult<Patient>> page(@Validated({BaseRequest.page.class}) PatientParam patientParam) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        patientParam.setBelongDoctor(userId);
        return new SuccessResponseData<>(this.service.findPage(patientParam));
    }
    @GetResource(
            name = "患者列表",
            path = {"/patients/reportPage"}
    )
    public ResponseData<PageResult<Patient>> reportPage(@Validated({BaseRequest.page.class}) PatientParam patientParam) {
        Long userId = LoginContext.me().getLoginUser().getUserId();
        patientParam.setBelongDoctor(userId);
        return new SuccessResponseData<>(this.service.findReportPage(patientParam));
    }
    @PostResource(
            name = "提交",
            path = {"/patients/report"}
    )
    @ApiLog
    public ResponseData<?> report(@RequestBody JSONObject disease) {
        this.service.saveAndReport(disease,2);
        return new SuccessResponseData<>();
    }
    @PostResource(
            name = "提交数据",
            path = {"/patients/temporarySave"}
    )

    public ResponseData<?> temporarySave(@RequestBody JSONObject disease) {
        this.service.saveAndReport(disease,1);
        return new SuccessResponseData<>();
    }
    @PostResource(
            name = "拉取医生",
            path = {"/patients/save"}
    )
    public ResponseData<?> save(@RequestBody JSONObject data) {
        if("his".equals(data.getString("type"))){

        }else{
            Long userId = LoginContext.me().getLoginUser().getUserId();

            SysUserOrg userOrg =  sysUserOrgMapper.selectOne(Wrappers.<SysUserOrg>lambdaQuery().eq(SysUserOrg::getUserId, userId));

            Patient patient = JSON.parseObject(JSON.toJSONString(data),Patient.class);
            patient.setStatus(0);
            patient.setBelongDoctor(userId);
            patient.setBelongOrg(userOrg.getOrgId());
            this.service.save(patient);
        }

        return new SuccessResponseData<>();
    }
}
